//
//  ViewController.swift
//  ImageColorPicker
//
//  Created by tanwz on 16/6/1.
//  Copyright © 2016年 tanwz. All rights reserved.
//

import Cocoa

class ViewController: NSViewController, DraggingViewDelegate {

    @IBOutlet weak var dragingView: DraggingView!
    @IBOutlet weak var imageView: NSImageView!
    @IBOutlet weak var sizeLb: NSTextField!
    @IBOutlet weak var colorLb: NSTextField!
    var pixelColor: NSColor?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        dragingView.delegate = self
        
//        dragingView.addTrackingRect(view.bounds, owner: self, userData: nil, assumeInside: true)
        colorLb.hidden = true
    }

    override var representedObject: AnyObject? {
        didSet {
        // Update the view, if already loaded.
        }
    }
    
    override func viewWillLayout() {
        super.viewWillLayout()
    }
    
    func DragedFiles(files: Array<NSString>) {
        if !files.isEmpty {
            if let file = files.first {
                let imageRep = NSImageRep(contentsOfFile: file as String)
                if imageRep == nil {
                    Swift.print("image rep is nil")
                    return
                }
                Swift.print(imageRep)
                
                sizeLb.cell?.title = "(\(Int(imageRep!.size.width)),\(Int(imageRep!.size.width)))"
                imageView.image = NSImage(contentsOfFile: file as String)
            }
        }
    }
    
    func MouseMoved(pos: CGPoint) {
        
        guard let offset = imageOffset() else {
            return
        }
        
        guard let color = imageView.image?.colorAtPoint(CGPointApplyAffineTransform(pos, CGAffineTransformMakeTranslation(-offset.x, -offset.y))) else {
            colorLb.hidden = true
            pixelColor = nil
            return
        }
        
        pixelColor = color
        colorLb.hidden = false
        colorLb.cell?.title = "rgb(\(Int(color.redComponent * 255)),\(Int(color.greenComponent * 255)),\(Int(color.blueComponent * 255)),\(Int(color.alphaComponent * 255)))"
//        colorLb.frame.origin = pos
        
    }
    
    func MouseDown(pos: CGPoint) {
        if pixelColor != nil {
            let colorStr = "\(Int(pixelColor!.redComponent * 255)),\(Int(pixelColor!.greenComponent * 255)),\(Int(pixelColor!.blueComponent * 255)),\(Int(pixelColor!.alphaComponent * 255))"
            
            let pasteBoard = NSPasteboard.generalPasteboard()
            pasteBoard.declareTypes([NSPasteboardTypeString], owner: nil)
            pasteBoard.setString(colorStr, forType: NSPasteboardTypeString)
        }
    }
    
    private func imageOffset() -> CGPoint? {
        guard let imageSize = imageView.image?.size else {
            return nil
        }
        
        return CGPointMake((CGRectGetWidth(imageView.frame) - imageSize.width) / 2,
                           (CGRectGetHeight(imageView.frame) - imageSize.height) / 2)
    }
}

